package com.jeff.tianti.controller.channel;

import com.jeff.tianti.cms.entity.Leader;
import com.jeff.tianti.cms.service.LeaderService;

import javax.servlet.http.HttpServletRequest;

import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.cms.dto.LeaderQueryDTO;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author NOW_GO
 * @desc LeaderController
 * @date 2019-07-12
 */
@Controller
@RequestMapping("/channel/leader")
public class LeaderController {

    @Autowired
    private LeaderService leaderService;

    @RequestMapping("/list")
    public String list(HttpServletRequest request, Model model) {
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");
        String name = request.getParameter("name");
        String position = request.getParameter("position");
        int currentPage = 1;
        int pageSize = 10;
        if (StringUtils.isNotBlank(currentPageStr)) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        LeaderQueryDTO leaderQueryDTO = new LeaderQueryDTO();
        leaderQueryDTO.setCurrentPage(currentPage);
        leaderQueryDTO.setPageSize(pageSize);
        leaderQueryDTO.setName(name);
        leaderQueryDTO.setPosition(position);
        PageModel<Leader> page = this.leaderService.queryLeaderPage(leaderQueryDTO);
        model.addAttribute("page", page);
        model.addAttribute(Constants.MENU_NAME, Constants.MESSAGE_LEADER);
        model.addAttribute("leaderQueryDTO", leaderQueryDTO);
        return "/channel/leader/list";
    }

    @RequestMapping("/add")
    public String add(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        Leader leader = null;
        if (StringUtils.isNotBlank(id)) {
            leader = this.leaderService.find(id);
        }
        model.addAttribute("leader", leader);
        model.addAttribute("id", id);
        return "/channel/leader/edit";
    }

    @RequestMapping("/saveorupdate")
    @ResponseBody
    public AjaxResult doSaveOrUpdate(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);

        try {
            String id = request.getParameter("id");
            String name = request.getParameter("name");
            String position = request.getParameter("position");
            Integer sort = Integer.parseInt(request.getParameter("sort"));
            Leader leader = null;
            if (StringUtils.isNotBlank(id)) {
                leader = this.leaderService.find(id);
                leader.setUpdateDate(new Date());
                leader.setName(name);
                leader.setPosition(position);
                leader.setSort(sort);
                this.leaderService.update(leader);
            } else {
                leader = new Leader();
                leader.setCreateDate(new Date());
                leader.setName(name);
                leader.setPosition(position);
                leader.setSort(sort);
                this.leaderService.save(leader);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @RequestMapping("/doDelete")
    @ResponseBody
    public AjaxResult doDelete(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            Leader leader = this.leaderService.find(id);
            if (leader != null) {
                leader.setDeleteFlag("1");
                this.leaderService.update(leader);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }


}